package com.eastfair.venuebooking.controller;

import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.venuebooking.dto.TemplateManageSaveDTO;
import com.eastfair.venuebooking.dto.TemplateManageUpdateDTO;
import com.eastfair.venuebooking.dto.TemplateManagePageQuery;
import com.eastfair.venuebooking.entity.TemplateManage;
import com.eastfair.venuebooking.service.TemplateManageService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import com.eastfair.venuebooking.vo.TemplateManageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;


/**
 * <p>
 * 前端控制器
 * 模板管理
 * </p>
 *
 * @author lyw
 * @date 2022-07-05
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/templateManage")
@Api(value = "TemplateManage", tags = "模板管理")
@PreAuth(replace = "venuebooking:templateManage:")
public class TemplateManageController extends SuperController<TemplateManageService, Long, TemplateManage, TemplateManagePageQuery, TemplateManageSaveDTO, TemplateManageUpdateDTO> {

    @Autowired
    private EchoService echoService;
    @Autowired
    private TemplateManageService templateManageService;

    @Override
    public void handlerResult(IPage<TemplateManage> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<TemplateManage> templateManageList = list.stream().map((map) -> {
            TemplateManage templateManage = TemplateManage.builder().build();
            //TODO 请在这里完成转换
            return templateManage;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(templateManageList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<TemplateManagePageQuery> params) {
        TemplateManagePageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }


    /**
     * 模板列表
     *
     * @return
     */
    @ApiOperation(value = "模板列表")
    @PostMapping(value = "/getTemplateList")
    public R<IPage<TemplateManageVO>> getTemplateList(@RequestBody PageParams<TemplateManagePageQuery> params) {
        IPage<TemplateManageVO> page = templateManageService.getTemplateList(params);
        return R.success(page);
    }



    /**
     * 添加模板
     *
     * @return
     */
    @ApiOperation(value = "添加模板")
    @SysLog("添加模板")
    @PostMapping(value = "/saveTemplateInfo")
    public R<Boolean> saveTemplateInfo(@RequestBody TemplateManageSaveDTO templateManageSaveDTO) throws Exception {

        return R.success(templateManageService.saveTemplateInfo(templateManageSaveDTO));
    }



    /**
     * 删除模板
     *
     * @return
     */
    @ApiOperation(value = "删除模板")
    @SysLog("删除模板")
    @GetMapping(value = "/delTemplateById/{id}")
    public R<Boolean> delTemplateById(@PathVariable Long id) {

        return R.success(templateManageService.delTemplateById(id));
    }


}
